package com.joshondesign.sdljava.gfx;

import com.joshondesign.sdljava.SDL_Event;

public class SliderNode extends Control {
    double percent;
    int height = 60;
    private int width = 200;
    private Callback callback;

    public SliderNode(Scene scene) {
        percent = 0.5;
        height = 60;
        width = 200;
        scene.listen(Trigger.MouseDragged().setCallback(new Callback<SDL_Event>(){
            public void call(SDL_Event sdl_event) {
                if(inside(sdl_event.getButton().getX(), sdl_event.getButton().getY(), x,y,width,height)) {
                    percent = (sdl_event.getMotion().getX()-x)/(double)width;
                    if(percent < 0) percent = 0;
                    if(percent > 1.0) percent = 1.0;
                    if(callback != null) {
                        callback.call(percent);
                    }
                }
            }
        }));
    }

    @Override
    public void draw(GFX gfx) {
        gfx.translate(x,y);
        // border
        gfx.setPaint(new SColor(220,150,100));
        gfx.fillRect(0,0, width,height);

        // background
        gfx.setPaint(new SColor(100,50,10));
        gfx.fillRect(1,1, width-2,height-2);

        // value
        gfx.setPaint(new SColor(220,150,100));
        gfx.fillRect(1,1, (int) (width*percent),height-2);

        // text
        gfx.setPaint(new SColor(255,255,255));
        gfx.drawString(this.font, (int)(percent*100)+"%" , 70,43);

        gfx.translate(-x,-y);
    }

    public double getValue() {
        return percent;
    }

    public void onChange(Callback callback) {
        this.callback = callback;
    }
}
